#include "videosender.h"
#include <QDebug>
#include <QBuffer>
#include <QString>
#include "messagepackage.h"

videoSender::videoSender(QString ip,int port,QObject *parent) : QObject(parent)
{
    this->ip=ip;
    this->port=port;
    socket = new QUdpSocket(this);
    socket->bind(QHostAddress::AnyIPv4,this->port,QAbstractSocket::ReuseAddressHint);
    socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption,300*2048);
    socket->joinMulticastGroup(QHostAddress(this->ip));
}

void videoSender::sendeVideo(QByteArray byte)
{

    int unitBytes = 1024;
    int byteCount = byte.length(); //图片大小
    int writeBytes = 0; //已写入大小
    int i=0;
    while(true)
    {
        ImagePackage pack;
        memset(&pack,0,sizeof(ImagePackage));
        pack.PackNum = i++;

        if(writeBytes<byteCount)
        {
            memcpy(pack.data,byte.data()+writeBytes,unitBytes);
            writeBytes += unitBytes;
            if(writeBytes>=byteCount)
            {
                pack.packTaken = unitBytes-(writeBytes-byteCount); //获取当前包实际的数据大小
                qDebug()<<byte.size()<<"count"<<byteCount;
            }
            else
            {
                pack.packTaken = unitBytes; //设置当前包中数据的大小
            }
            socket->writeDatagram((char*)&pack,sizeof(ImagePackage),QHostAddress(this->ip),this->port);
        }
        else
        {
            break;
        }
    }
}

